function MainAssistant() {};

MainAssistant.prototype.setup = function() {
    this.debugContainer = this.controller.get("debugOutput");
    this.testDiv = this.controller.get("testDiv");
    this.logOutputNum = 0;
    
// Setup Widgets// User id	
var lastUser = getLocalData("dname");
var lastpwd = getLocalData("dpwd");
this.useridModel = { value: "" };
	if(lastUser){
    this.useridModel.value=lastUser;
	}
this.controller.setupWidget("login_txtUserid",
              this.attributes = {
                  hintText: "Enter User Id",
                  multiline: false,
                  textCase: Mojo.Widget.steModeLowerCase,
                  enterSubmits: false,
                  focusMode: Mojo.Widget.focusSelectMode,
                  focus: true
              },
              this.useridModel
            );
// Password
	this.passwordModel = { value: "" };
	if(lastpwd){
	this.passwordModel.value = lastpwd;
	}
	this.controller.setupWidget("login_txtPassword",
			  this.attributes = {
			      hintText: "Type Password",
				  enterSubmits: true
			  },
			  this.passwordModel
			); 

// Set up Buttons
// login_btnSignin button	
	this.controller.setupWidget("login_btnSignin", { type: Mojo.Widget.activityButton },
		    {
			label : "Login",
			buttonClass : "buttonfloat affirmative",
		    }
		  ); 
	Mojo.Event.listen(this.controller.get("login_btnSignin"), Mojo.Event.tap, 
				    this.callLogin.bind(this)
				  ); 
				  
};

MainAssistant.prototype.callLogin = function(event) {

   setLocalData("dname",this.useridModel.value);
   setLocalData("dpwd",this.passwordModel.value);
    var f = new Future();

    var configObj = configObject;

    callTransService(configObj, 'login', {"USER":this.useridModel.value,"PASSWORD": this.passwordModel.value}, f);
    
    f.then( function(future) {
        //this.logMsg( "code : " + future.result.code +", Service Return: " +JSON.stringify(future.result.reply));  
        //Mojo.Log.info(" future result ", JSON.stringify(future.result));
		if(future && future.result && future.result.reply[0] && future.result.reply[0].username){
		    this.logMsg("future.result.reply"+future.result.reply);
			username = future.result.reply[0].username;
			setLocalData("lastuser",username);	
			islogin = true;			
		} else {
		    this.logMsg("future:"+JSON.stringify(future));
		    username = getLocalData("lastuser");
			islogin = false;	
		}
		
		Mojo.Controller.stageController.swapScene({name : "periodlist",disableSceneScroller : false});		
    }.bind(this));
	
};


MainAssistant.prototype.activate = function(event) {
    var that = this;
    this.logMsg("Setup");
	
    // Call the Hello World service using standard Palm serviceRequest
    //com.bbhome.testservice.service
    //com.hpit.cdc.pagetrans.service
   this.logMsg("beging load configuration");
   
   var f = new Future();
   
   getConfiguration('PPM_engine_config.json', this.controller, f);
   //getConfiguration('ITRTT_engine_config.json', this.controller, f);
   //getConfiguration('HPED_engine_config.json', this.controller, f);
   //getConfiguration('SRPA_engine_config.json', this.controller, f);
   //getConfiguration('athp_engine_config.json', this.controller, f);
   //getConfiguration('INFRASEC_engine_config.json', this.controller, f);
   //getConfiguration('News_engine_config.json', this.controller, f);
   
   f.then( function(future) {
     this.loadConfigSuc(future.result);
   }.bind(this));

};

MainAssistant.prototype.loadConfigSuc = function(configObj) {
   configObject = configObj.reply;
   Mojo.Log.info("in success function configObj " + JSON.stringify(configObj));
   this.logMsg("successfully load configuration instance " + configObject.instance_name);
};

MainAssistant.prototype.loadConfigFail = function() {
   Mojo.Log.info("configObj failed");
   this.logMsg("configObj failed");
};


MainAssistant.prototype.serviceSuccess = function(successData){
    this.logMsg("Success Data: " + JSON.stringify(successData) + "  ");
    
    Mojo.Log.info("Success Data: " + JSON.stringify(successData) + "  ");
    
    configObj = successData.reply;
    
//    this.controller.serviceRequest("palm://com.hpit.cdc.pagetrans.service", {
//        method: "process",
//        parameters: {'servicename': 'testHeader'},
//        onSuccess:this.serviceSuccess2.bind(this),
//        onFailure:this.serviceFailure.bind(this)
//    });
};
MainAssistant.prototype.serviceFailure = function(failData){
    this.logMsg("Fail Data:" + JSON.stringify(failData));
};

MainAssistant.prototype.deactivate = function(event) {};

MainAssistant.prototype.cleanup = function(event) {};

MainAssistant.prototype.logMsg = function(logText) {
    //this.debugContainer.innerHTML = "" + (this.logOutputNum++) + ": " + StringUtils.escapeHTML(logText) + "<br/>" + this.debugContainer.innerHTML;
    //this.debugContainer.innerHTML = (this.logOutputNum++) + ": " + logText + "" + this.debugContainer.innerHTML;            
};    

MainAssistant.prototype.serviceSuccess2 = function(successData){ 
  //this.logMsg("Success2 Data: " + JSON.stringify(successData) + "  ");
  Mojo.Log.info("Success2 Data: " + JSON.stringify(successData) + "  ");
}
